package com.jbm.jbmsupplier.zp.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.net.NetInterChat;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.view.DoubleButtonDialog;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;
import com.jbm.onehome.iface.ContactEntity;
import com.jbm.onehome.iface.LoginEntity;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

import static com.jbm.jbmsupplier.zp.utils.TableName.BUDDY;

/**
 * Created by 家帮忙 on 2017/5/23.
 */

public class MyAddcononet extends BaseAdapter {
    private Context context;
private ArrayList<LoginEntity> arrlogin;
    private DialogLoading dialogLoading;
    private final int ADDFRIEND = 0002;
    private Handler handlers;
    private int positions;

    public MyAddcononet(Context context, ArrayList<LoginEntity> arrlogin, Handler handlers) {
        this.context = context;
        this.arrlogin = arrlogin;
        this.handlers=handlers;
    }

    @Override
    public int getCount() {
        return arrlogin.size();
    }

    @Override
    public Object getItem(int position) {
        return arrlogin.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myholder=null;
        if (convertView==null){

            convertView=View.inflate(context, R.layout.addconontact_item_iz,null);
            myholder=  new MyViewHolder(convertView);
            convertView.setTag(myholder);
        }else {
           myholder=(MyViewHolder) convertView.getTag();
        }
        positions=position;
        parent.setTag(arrlogin.get(position));
        myholder.text_item.setText(arrlogin.get(position).getNickname());
        if (arrlogin.get(position).getLogo().equals("null")){
            myholder.image_item.setImageDrawable(context.getResources().getDrawable(R.mipmap.pic));
        }else{
            ImageOptions i=new ImageOptions.Builder().setCircular(true).build();
            x.image().bind( myholder.image_item,arrlogin.get(position).getLogo(),i);
        }

        myholder.btn_sendmsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (new ClassUser(context).getLogin()== arrlogin.get(position).getLoginId()){
                    Toast.makeText(context, "不能添加自己为好友", Toast.LENGTH_SHORT).show();

                }else{
                NetInterChat netinterchat = new NetInterChat(handler);
                netinterchat.addFriendCount(ADDFRIEND, new ClassUser(context).getLogin(), arrlogin.get(position).getLoginId());
                    dialogLoading = new DialogLoading(context, "添加中.....", false);
                    dialogLoading.show();
            }}
        });

        return convertView;
    }

    private class MyViewHolder{
        private ImageView image_item;
        private TextView  text_item;
        private Button btn_sendmsg;
        public MyViewHolder(View convertView) {
            image_item= (ImageView)convertView.findViewById(R.id.nameimage);
            text_item= (TextView) convertView.findViewById(R.id.tv_name);
            btn_sendmsg= (Button) convertView.findViewById(R.id.btn_sendmsg);
        }


    }
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                case ADDFRIEND:
                    final ContactEntity accctrl = (ContactEntity) msg.obj;
                    if (accctrl.m_info.equals("OK")) {
                            MySqliteUtlis.GetInstan(context).StoringBuddydata(BUDDY, accctrl.getLoginId(),  accctrl.getNickname(),  accctrl.getAccount(),  accctrl.getMobile(), "",  accctrl.getLogo(), "", "", "", 1, new ClassUser(context).getLogin());
                            if (MySqliteUtlis.GetInstan(context).showFriendsToID(BUDDY, accctrl.getLoginId(),new ClassUser(context).getLogin()).getIsfriends() == 0) {
                                MySqliteUtlis.GetInstan(context).UpdateIsfriends(BUDDY,  accctrl.getLoginId());
                            }

                        setDialogding();
                        Intent intent = new Intent();
                        intent.putExtra("messageFlag", 50);
                        intent.setAction("AddContact");
                       context. sendBroadcast(intent);
                        handlers.sendMessage(handlers.obtainMessage(0));
                        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                    } else if (accctrl.m_info.equals("已存在")){
                        hint("账号已存在");
                        setDialogding();
                    }else{
                        setDialogding();
                    }
                    break;
            }
        }
    };

    public void setDialogding() {
        if (dialogLoading != null) {
            dialogLoading.dismiss();
        }
    }
    //一些提示的时候调用
    public void hint(String name) {
        final DoubleButtonDialog dialog = new DoubleButtonDialog(
               context, name);
        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();

            }
        });
        dialog.setisVisibility();

    }
}
